package net.zdsoft.wpchat.support.action;

import javax.annotation.Resource;

import net.zdsoft.keel.action.ActionContext;
import net.zdsoft.keel.util.Validators;
import net.zdsoft.wpchat.android.client.BasicAction;
import net.zdsoft.wpchat.support.client.MemoryUser;
import net.zdsoft.wpchat.support.entity.Operator;
import net.zdsoft.wpchat.support.entity.UserConfig;
import net.zdsoft.wpchat.support.service.AuthService;
import net.zdsoft.wpchat.support.service.OperatorService;
import net.zdsoft.wpchat.support.service.SysOptionService;

/**
 * 用户登录登出action
 * 
 * @author xuan
 * @version $Revision: 1.0 $, $Date: 2014-9-22 下午8:19:37 $
 */
public class LoginAction extends BasicAction {
    private static final long serialVersionUID = -6296020554022480501L;

    private String username;
    private String password;

    @Resource
    private OperatorService operatorService;
    @Resource
    private SysOptionService sysOptionService;
    @Resource
    private AuthService authService;

    /**
     * 进入登录页面，判断用户是否已登录，没有就跳转到登录页面
     * 
     * @return
     */
    public String init() {
        Boolean loginState = (Boolean) ActionContext.getSession().getAttribute(LOGIN);
        if (loginState != null && loginState.booleanValue()) {
            addActionError("已经超时请重新登录");
        }

        return SUCCESS;
    }

    /**
     * 点击登录，进行用户名密码判断，并进行登录
     * 
     * @return
     */
    public String login() {
        ActionContext.getSession().removeAttribute(LOGIN);
        if (Validators.isEmpty(username) || Validators.isEmpty(password)) {
            addActionError("请输入用户名和密码");
            return INPUT;
        }

        Operator operator = operatorService.getOperatorByUserNameAndPwd(username, password);
        if (operator == null) {
            addActionError("用户名不存在或密码错误");
            return INPUT;
        }
        if (operator.getState() == 0) {
            addActionError("对不起，用户被冻结，无法登录");
            return INPUT;
        }
        MemoryUser memoryUser = new MemoryUser();
        memoryUser.setId(operator.getId());
        memoryUser.setUsername(operator.getLoginName());
        memoryUser.setRealName(operator.getName());
        memoryUser.setType(operator.getType());
        memoryUser.setRegionRule(operator.getRegionRule());

        // 设置该用户的权限配置
        UserConfig userConfig = authService.getUserConfig(operator.getId());
        if (userConfig == null) {
            addActionError("对不起，您不属于任何组，无法登陆");
            return INPUT;
        }

        memoryUser.setUserConfig(userConfig);
        ActionContext.getSession().setAttribute(MemoryUser.KEY, memoryUser);

        return SUCCESS;
    }

    /**
     * 用户退出登录
     * 
     * @return
     */
    public String logout() {
        ActionContext.getSession().removeAttribute(MemoryUser.KEY);
        ActionContext.getSession().invalidate();
        return SUCCESS;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
